package client.gui.panels;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

import controllers.ControllerIF;
import entities.GeneralInformationStage;
import entities.InfoSystem;
import entities.Ticket;
import entities.User;
		

public class TimeExtensionList extends JPanel implements ActionListener, PanelIF, MouseListener, ListSelectionListener  {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTable table;
	private DefaultTableModel model;
	private ArrayList<Ticket> tickets;
	private Ticket ticket;
	private User user;
	private JFrame frame;


	public TimeExtensionList() {
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		new ViewTicket(ticket, null);
		user = new User();
		
		Component verticalStrut = Box.createVerticalStrut(20);
		add(verticalStrut);
		
		
		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new TitledBorder(null, "Time extension list:", TitledBorder.LEADING, TitledBorder.TOP, new Font("Tahoma", Font.BOLD, 14), null));
		add(panel_3);
				table = new JTable(model);		
				panel_3.add(table);
				table.setPreferredSize(new Dimension(650, 400));
				table.setFont(new Font("Tahoma", Font.PLAIN, 12));
				table.setBorder(new LineBorder(new Color(0, 0, 0),2));
				// model.setRowCount(3);
				table.setModel(new DefaultTableModel(
					new Object[][] {
						{"Ticket ID", "Requester ID", "Requester name", "Current status", "Date", "Description"},
						{null, null, null, null, null, null},
						{null, null, null, null, null, null},
						{null, null, null, null, null, null},
						{null, null, null, null, null, null},
						{null, null, null, null, null, null},
						{null, null, null, null, null, null},
						{null, null, null, null, null, null},
						{null, null, null, null, null, null},
						{null, null, null, null, null, null},
						{null, null, null, null, null, null},
						{null, null, null, null, null, null},
						{null, null, null, null, null, null},
						{null, null, null, null, null, null},
						{null, null, null, null, null, null},
						{null, null, null, null, null, null},
						{null, null, null, null, null, null},
						{null, null, null, null, null, null},
						{null, null, null, null, null, null},
						{null, null, null, null, null, null},
						{null, null, null, null, null, null},
						{null, null, null, null, null, null},
						{null, null, null, null, null, null},
						{null, null, null, null, null, null},
						{null, null, null, null, null, null},
						{null, null, null, null, null, null},
					},
					new String[] {
						"Ticket ID", "Requester name", "Request ID", "Current status", "Date", "Description"
					}
				) {
					/**
					 * 
					 */
					private static final long serialVersionUID = 1L;
					boolean[] columnEditables = new boolean[] {
						false, false, false, false, false, false
					};
					public boolean isCellEditable(int row, int column) {
						return columnEditables[column];
					}
				});
				table.getColumnModel().getColumn(0).setMaxWidth(100);
				table.getColumnModel().getColumn(1).setMaxWidth(100);
				table.getColumnModel().getColumn(2).setMaxWidth(110);
				table.getColumnModel().getColumn(3).setMaxWidth(120);
				table.getColumnModel().getColumn(4).setMaxWidth(130);
				table.getColumnModel().getColumn(5).setMaxWidth(188);
				
				//scrollPane = new JScrollPane(table);
				
				table.getTableHeader();
				
						table.addMouseListener(this);
						table.getSelectionModel().addListSelectionListener(table);
				
				table.getModel().addTableModelListener(new TableModelListener() {
	        public void tableChanged(TableModelEvent e) {
	          /*  if(e.getType() == e.UPDATE){
	                System.out.println(e.getColumn());
	                System.out.println(e.getFirstRow());
	                System.out.println(e.getLastRow());
	            }*/
	        }
	    });
	
		
		ticket = new Ticket();
		
		ticket.setTicket_id((long)123456789);
		ticket.setInfoSystem(new InfoSystem("system name"));
		GeneralInformationStage generalInfo = new GeneralInformationStage();
		generalInfo.setInitDate("11/11/11");
		generalInfo.setRequestedChangedDescription("Requested Changed Description");
		ticket.setStatus("STATUS");
		user.setId((long)987654321);
		
		user.setFirstName("Anna");
		user.setLastName("Karenina");

		ticket.setState("stage name");
		
		
		for (int i=1; i<25; i++){
			int j=0;
			table.setValueAt(ticket.getTicket_id() , i, j);
			table.setValueAt(ticket.getInfoSystem() , i, j+1);
			table.setValueAt(generalInfo.getInitDate() , i, j+2);
			table.setValueAt(generalInfo.getRequestedChangedDescription() , i, j+3);
			table.setValueAt(ticket.getStatus() , i, j+4);
			table.setValueAt(user.getId(), i, j+5);
		/*	table.setValueAt(user.getFirstName() + " " + user.getLastName() , i, j+6);
			table.setValueAt(ticket.getState() , i, j+7); */
		}
		
	}
		Component verticalStrut_3 = Box.createVerticalStrut(20);
		// add(verticalStrut_3);
		

		
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		}
		
		/*else if(e.getSource().equals(clearButton)) {
			usernameTextField.setText("");
			passwordField.setText("");
		}*/
	
	
	@Override
	public void mousePressed(MouseEvent e) {
		if (e.getClickCount() == 1){
			System.out.println("mouse");
			System.out.println(table.getSelectedRow());
			System.out.println(table.getValueAt(table.getSelectedRow(), 0));
		/*	for (int i=1; i<21; i++){
				for (int j=0; j<7; j++){			
					 table.setValueAt(i+j, i, j);
				}
			} */
			
		}
		
		if (e.getClickCount() == 2){
			if (!table.getValueAt(1, 0).equals(null)){
				
				int response = JOptionPane.showConfirmDialog(frame, "Do you want to approve time extension?", "Question", JOptionPane.YES_NO_OPTION);
				if (response == JOptionPane.YES_OPTION) {
					System.out.println("YES!");
					System.out.println(JOptionPane.YES_OPTION);
				}
				else if (response == JOptionPane.NO_OPTION){
					System.out.println(JOptionPane.NO_OPTION);

			}
			
		}
			
				
			//	viewTicket.GeneralDetails.append(("\n\n" + table.getValueAt(table.getSelectedRow(), 0).toString()));
			/*	this.removeAll();
				this.add(viewTicket);
				validate();
				setVisible(true); */
			}
				
           // table.getCellEditor().cancelCellEditing();
		}


	


	@Override
	public void setController(ControllerIF cif) {
		
	}


	@Override
	public void valueChanged(ListSelectionEvent e) {}


	@Override
	public void mouseClicked(MouseEvent arg0) {}


	@Override
	public void mouseEntered(MouseEvent arg0) {}


	@Override
	public void mouseExited(MouseEvent arg0) {}


	@Override
	public void mouseReleased(MouseEvent arg0) {}


	public ArrayList<Ticket> getTickets() {
		return tickets;
	}


	public void setTickets(ArrayList<Ticket> tickets) {
		this.tickets = tickets;
	}

	
}
	
